package com.web.exiaodao.business.controller;

import com.web.exiaodao.business.service.WalletWithdrawRecordService;
import com.web.exiaodao.remote.vo.WalletWithdrawRecordVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: WalletWithdrawRecordController.java
 * @Package com.web.webstart.business.controller
 * @Description: 钱包-提现记录控制器
 * @date 2017-02-06 11:44:38
 */
@Controller
@RequestMapping("/cms/walletWithdrawRecord")
public class WalletWithdrawRecordController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(WalletWithdrawRecordController.class);

    @Autowired
    private WalletWithdrawRecordService walletWithdrawRecordService;

    /**
     * @param pageSize
     * @param status     //默认查询非删除3的数据,具体类型参照XaConstant.Status
     *                   //@param sortData
     * @param jsonFilter
     * @return
     * @Title: findWalletWithdrawRecordNEStatusPage
     * @Description: 分页查询WalletWithdrawRecord     * @param nextPage
     */
    @ResponseBody
    @RequestMapping(value = "findWalletWithdrawRecordNEStatusPage", method = RequestMethod.POST)
    public XaResult<Page<WalletWithdrawRecordVo>> findWalletWithdrawRecordNEStatusPage(
            @RequestParam(defaultValue = "0") Integer nextPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "3") Integer status,
            @RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
            @RequestParam(defaultValue = "{}") String jsonFilter
    ) throws BusinessException {
        XaResult<Page<WalletWithdrawRecordVo>> xr = new XaResult<Page<WalletWithdrawRecordVo>>();
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith(jsonFilter, "search_");
        xr = walletWithdrawRecordService.findListNEStatusByFilter(status, filterParams, pageable);
        return xr;
    }

    /**
     * @param pageSize
     * @param status     //默认查询正常状态1的数据,具体类型参照XaConstant.Status
     *                   //@param sortData
     * @param jsonFilter
     * @return
     * @Title: findWalletWithdrawRecordEQStatusPage
     * @Description: (预留)分页查询WalletWithdrawRecord     * @param nextPage
     */
    @ResponseBody
    @RequestMapping(value = "findWalletWithdrawRecordEQStatusPage", method = RequestMethod.POST)
    public XaResult<Page<WalletWithdrawRecordVo>> findWalletWithdrawRecordEQStatusPage(
            @RequestParam(defaultValue = "0") Integer nextPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer status,
            @RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
            @RequestParam(defaultValue = "{}") String jsonFilter
    ) throws BusinessException {
        XaResult<Page<WalletWithdrawRecordVo>> xr = new XaResult<Page<WalletWithdrawRecordVo>>();
        try {
            Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
            Map<String, Object> filterParams = WebUitl.getParametersStartingWith(jsonFilter, "search_");
            xr = walletWithdrawRecordService.findListEQStatusByFilter(status, filterParams, pageable);
        } catch (Exception e) {
            xr.error(e instanceof BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage(), new Throwable(e));
        }
        return xr;
    }

    /**
     * @param modelId
     * @return
     * @Title: findWalletWithdrawRecordById
     * @Description: 根据ID查找单条实体
     */
    @ResponseBody
    @RequestMapping(value = "findWalletWithdrawRecordById", method = RequestMethod.POST)
    public XaResult<WalletWithdrawRecordVo> findWalletWithdrawRecordById(
            @RequestParam Long modelId
    ) throws BusinessException {
        XaResult<WalletWithdrawRecordVo> xr = new XaResult<WalletWithdrawRecordVo>();
        try {
            xr = walletWithdrawRecordService.findOne(modelId);
        } catch (Exception e) {
            xr.error(e instanceof BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage(), new Throwable(e));
        }
        return xr;
    }

    /**
     * @param model
     * @return
     * @Title: saveModel
     * @Description: 保存实体数据
     */
    @ResponseBody
    @RequestMapping(value = "saveWalletWithdrawRecord", method = RequestMethod.POST)
    public XaResult<WalletWithdrawRecordVo> saveWalletWithdrawRecord(
            HttpServletRequest request,
            WalletWithdrawRecordVo model
    ) throws BusinessException {
        XaResult<WalletWithdrawRecordVo> xr = new XaResult<WalletWithdrawRecordVo>();
        try {
            model.setCreateUser(request.getSession().getAttribute(XaConstant.SessionKey.currentUser).toString());
            xr = walletWithdrawRecordService.saveOrUpdate(model);
        } catch (Exception e) {
            xr.error(e instanceof BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage(), new Throwable(e));
        }
        return xr;
    }

    /**
     * @param modelIds 多个实体id,中间用逗号隔开
     * @param status   操作类型,status类型见XaConstant.Status,默认删除操作
     * @return
     * @Title: multiOperateWalletWithdrawRecordByIds
     * @Description: 批量操作多个实体状态, 根据status进行操作
     */
    @ResponseBody
    @RequestMapping(value = "operateWalletWithdrawRecordByIds", method = RequestMethod.POST)
    public XaResult<WalletWithdrawRecordVo> operateWalletWithdrawRecordByIds(
            HttpServletRequest request,
            @RequestParam String modelIds,
            @RequestParam(defaultValue = "3") Integer status
    ) throws BusinessException {
        XaResult<WalletWithdrawRecordVo> xr = new XaResult<>();
        try {
            xr = walletWithdrawRecordService.multiOperate(modelIds, status);
        } catch (Exception e) {
            xr.error(e instanceof BusinessException ? e.getMessage() : XaConstant.Message.error);
            LOGGER.error(XaConstant.Message.error + e.getMessage(), new Throwable(e));
        }
        return xr;
    }

    /**
     * 支付宝提现申请
     *
     * @author jack
     * @date 2017/2/7 下午2:00
     */
    @ResponseBody
    @RequestMapping(value = "batchTransByAliPay", method = RequestMethod.POST)
    public XaResult<String> withdrawById(
            HttpServletRequest request,
            @RequestParam String modelIds
    ) throws BusinessException {
        XaResult<String> xr = new XaResult<>();
        if (XaUtil.isEmpty(modelIds)) {
            xr.error("modelIds不能为空!");
            return xr;
        }
        return walletWithdrawRecordService.withdrawOnAlipay(modelIds, request);
    }

}

